package com.bawei.module_home.viewmodel

import androidx.lifecycle.viewModelScope
import com.bawei.lib_common.base.utils.LoggerUtils
import com.bawei.lib_common.entity.VideoCommentEntityItem
import com.bawei.lib_mvi.base.BaseViewModel
import com.bawei.module_home.intent.SimpleTypeIntent
import com.bawei.module_home.model.repository.HomeRepository
import com.bawei.module_home.state.SimpleTypeState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch

class HomeViewModel : BaseViewModel<SimpleTypeIntent>(){
    private val repository: HomeRepository by  lazy { HomeRepository() }
    private var simpleTypeFlow = MutableStateFlow<SimpleTypeState>(SimpleTypeState.Init)
    val _simpleTypeFlow = simpleTypeFlow.asStateFlow()
    private var simpleVideoFlow = MutableStateFlow<SimpleTypeState>(SimpleTypeState.Init)
    val _simpleVideoFlow = simpleVideoFlow.asStateFlow()
    private var simpleCommentFlow = MutableStateFlow<SimpleTypeState>(SimpleTypeState.Init)
    val _simpleCommentFlow = simpleCommentFlow.asStateFlow()
    private var simpleSCommentFlow = MutableStateFlow<SimpleTypeState>(SimpleTypeState.Init)
    val _simpleSCommentFlow = simpleSCommentFlow.asStateFlow()
    private var simpleDCommentFlow = MutableStateFlow<SimpleTypeState>(SimpleTypeState.Init)
    val _simpleDCommentFlow = simpleDCommentFlow.asStateFlow()
    private var simpleYesLoveCommentFlow = MutableStateFlow<SimpleTypeState>(SimpleTypeState.Init)
    val _simpleYesLoveCommentFlow = simpleYesLoveCommentFlow.asStateFlow()
    private var simpleNOLoveCommentFlow = MutableStateFlow<SimpleTypeState>(SimpleTypeState.Init)
    val _simpleNOLoveCommentFlow = simpleNOLoveCommentFlow.asStateFlow()
    private var simpleAddFlow = MutableStateFlow<SimpleTypeState>(SimpleTypeState.Init)
    val _simpleAFlow = simpleAddFlow.asStateFlow()
    //1:根据意图发起网络请求
    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect{
                when(it){
                    is SimpleTypeIntent.getSimpleType-> getSimpleTypeData()
                    is SimpleTypeIntent.getSimpleVideoByChannelId -> getSimpleVideoByChannelId(it)
                    is SimpleTypeIntent.getCommentByUserId -> getCommentByUserId(it)
                    is SimpleTypeIntent.agreeComment -> agreeComment(it)
                    is SimpleTypeIntent.cancelAgreeComment -> cancelAgreeComment(it)
                    is SimpleTypeIntent.deleteComment -> deleteComment(it)
                    is SimpleTypeIntent.sendComment -> sendComment(it)
                    is SimpleTypeIntent.updateAccountAmount -> updateAccountAmount(it)
                }
            }
        }
    }
    //2:根据网络请求结果更新状态流
    private fun getSimpleTypeData() {
        viewModelScope.launch {
            repository.getSimpleType()
                .catch { e->
                    simpleTypeFlow.value = SimpleTypeState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        if(it.data.isEmpty()){
                            simpleTypeFlow.value = SimpleTypeState.Empty
                        }else{
                            simpleTypeFlow.value = SimpleTypeState.SimpleTypeSuccess(it.data)
                        }
                    }else{
                        simpleTypeFlow.value = SimpleTypeState.Failed(it.msg)
                    }
                }

        }
    }
    private fun getSimpleVideoByChannelId(it : SimpleTypeIntent.getSimpleVideoByChannelId) {
        viewModelScope.launch {
            repository.getSimpleVideoByChannelId(it.channelId,it.page,it.pageSize)
                .catch { e->
                    simpleVideoFlow.value = SimpleTypeState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        if(it.data.isEmpty()){
                            simpleVideoFlow.value = SimpleTypeState.Empty
                        }else{
                            simpleVideoFlow.value = SimpleTypeState.SimpleVideoSuccess(it.data)
                        }
                    }else{
                        simpleVideoFlow.value = SimpleTypeState.Failed(it.msg)
                    }
                }
        }
    }
    fun getCommentByUserId(it: SimpleTypeIntent.getCommentByUserId){
        viewModelScope.launch {
            repository.getCommentByUserId(it.datatype,it.itemid)
                .catch { e->
                    simpleCommentFlow.value = SimpleTypeState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        if(it.data.isEmpty()){
                            simpleCommentFlow.value = SimpleTypeState.Empty
                        }else{
                            simpleCommentFlow.value = SimpleTypeState.SimpleVideoCommentSuccess(it.data)
                        }
                    }else{
                        simpleCommentFlow.value = SimpleTypeState.Failed(it.msg)
                    }
                }
        }
    }
    /**
     * 发送评论
     */
    fun sendComment(it: SimpleTypeIntent.sendComment) {
        viewModelScope.launch {
            repository.sendComment(it.body)
                .catch { e->
                    simpleSCommentFlow.value = SimpleTypeState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        if(it.data.itemid.isEmpty()){
                            simpleSCommentFlow.value = SimpleTypeState.Empty
                        }else{
                            simpleSCommentFlow.value = SimpleTypeState.SimpleVideoCommentSendSuccess(it.data)
                        }
                    }else{
                        simpleSCommentFlow.value = SimpleTypeState.Failed(it.msg)
                    }
                }
        }
    }
    /**
     * 删除评论
     */
    fun deleteComment(it: SimpleTypeIntent.deleteComment) {
        viewModelScope.launch {
            repository.deleteComment(it.commentid)
                .catch { e->
                    simpleDCommentFlow.value = SimpleTypeState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        simpleDCommentFlow.value = SimpleTypeState.SimpleVideoCommentDeleteSuccess(it.data)
                    }else{
                        simpleDCommentFlow.value = SimpleTypeState.Failed(it.msg)
                    }
                }
        }
    }
    /**
     * 点赞评论
     */
    fun agreeComment(it: SimpleTypeIntent.agreeComment) {
        viewModelScope.launch {
            repository.agreeComment(it.commentid)
                .catch { e->
                    simpleYesLoveCommentFlow.value = SimpleTypeState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        simpleYesLoveCommentFlow.value = SimpleTypeState.SimpleVideoCommentAgreeSuccess(it.data)
                    }else{
                        simpleYesLoveCommentFlow.value = SimpleTypeState.Failed(it.msg)
                    }
                }
        }
    }
    /**
     * 取消点赞
     */
    fun cancelAgreeComment(it: SimpleTypeIntent.cancelAgreeComment) {
        viewModelScope.launch {
            repository.cancelAgreeComment(it.commentid)
                .catch { e->
                    simpleNOLoveCommentFlow.value = SimpleTypeState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        simpleNOLoveCommentFlow.value = SimpleTypeState.SimpleVideoCommentCancelAgreeSuccess(it.data)
                    }else{
                        simpleNOLoveCommentFlow.value = SimpleTypeState.Failed(it.msg)
                    }
                }
        }
    }

    //红包自定义view 更新账户金额
    fun updateAccountAmount(it: SimpleTypeIntent.updateAccountAmount) {
        viewModelScope.launch {
            repository.updateAccountAmount(it.id,it.nm)
                .catch { e->
                    simpleAddFlow.value = SimpleTypeState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        simpleAddFlow.value = SimpleTypeState.SimpleVideoCommentCancelAgreeSuccess(it.data)
                    }else{
                        simpleAddFlow.value = SimpleTypeState.Failed(it.msg)
                    }
                }
        }
    }
}